<template>
  <div >
    <video :src="background" autoplay loop muted class="backendBackground"></video>

    <div id="loginDiv">
      <form id="form">
        <h1 id="loginMsg">LOGIN IN</h1>
        <p>Username:<input id="username" name="username" type="text"  v-model="state.username"></p>
        <p>Password:<input id="password" name="password" type="password"  v-model="state.password"  @keyup.enter="onLogin"></p>

        <div id="subDiv">
          <input type="button" class="button" value="login up" @click="onLogin()">
          <input type="button" class="button" value="reset" @click="onReset()">
        </div>
      </form>
    </div>
  </div>

</template>

<script setup>
import background from '@/assets/video/background.mp4';
import {reactive} from "vue";
import { useRouter } from "vue-router";
import {ElMessage} from "element-plus";
import {login} from "@/api/backend";
import {getBackendType, setBackendName, setBackendToken, setBackendType, setBackendUid} from "@/utils/auth";

const router = useRouter();

const state = reactive({
  username: "",
  password: ""
});

/**
 * 登录方法
 */
const onLogin = async () => {
  if (!state.username) {
    ElMessage.error("用户名不能为空！");
    return;
  }
  if (!state.password) {
    ElMessage.error("密码不能为空！");
    return;
  }

  const {data} = await login(state);

  console.log(data);

  setBackendUid(data.uid);
  setBackendName(data.name);
  setBackendToken(data.token);
  setBackendType(data.type);

  router.push({
    path: "/backend/"
  });
}

const onReset = () => {
  state.username = "";
  state.password = "";
}

</script>

<style scoped lang="scss">

.backendBackground {
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
  object-fit: cover;
}


#loginDiv {
  width: 37%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  background-color: rgba(75, 81, 95, 0.3);
  box-shadow: 7px 7px 17px rgba(52, 56, 66, 0.5);
  border-radius: 5px;

  position: relative;
  top: 200px;
  left: 500px;
}
p {
  margin-top: 30px;
  margin-left: 20px;
  color: azure;
}
input {
  margin-left: 15px;
  border-radius: 5px;
  border-style: hidden;
  height: 30px;
  width: 140px;
  background-color: rgba(216, 191, 216, 0.5);
  outline: none;
  color: #f0edf3;
  padding-left: 10px;
}
#username{
  width: 200px;
}
#password{
  width: 200px;
}
.button {
  border-color: cornsilk;
  background-color: rgba(100, 149, 237, .7);
  color: aliceblue;
  border-style: hidden;
  border-radius: 5px;
  width: 100px;
  height: 31px;
  font-size: 16px;
}
#subDiv {
  text-align: center;
  margin-top: 30px;
}
#loginMsg{
  text-align: center;color: aliceblue;
}
</style>